<template>
  <div>
    <div class="attribute-title">Artiflow树</div>
    <el-tree
      :data="treeData"
      :props="defaultProps"
      @node-click="handleNodeClick"
      default-expand-all
    ></el-tree>
        <el-dialog title="Artifact类" :visible.sync="artifactDialogVisible" width="50%">
      <span slot="footer" class="dialog-footer">
        <el-form ref='form' :model="form" label-width="80px">
          <el-form-item label="标签">
            <el-input v-model="form.label"></el-input>
          </el-form-item>
          <el-form-item label="属性">
            <div class="tag-list">
            <el-tag :key="attr" v-for="attr in form.attributeList">{{attr}}</el-tag>
            </div>
          </el-form-item>
          <el-form-item label="生命周期">
            <div class="tag-list">
            <el-tag :key="state" v-for="state in form.stateList">{{state}}</el-tag>
                        </div>
          </el-form-item>
        </el-form>
        <el-button @click="artifactDialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>

</template>

<script>
import Utils from '../Utils'
import eventBus from "../EventBus";
export default {
  name: "DataTree",
  data() {
    return {
      artifactDialogVisible: false,
      form: {},
      treeData: [],
      defaultProps: {
        children: "children",
        label: "label",
      },
    }
  },
  methods: {
    handleNodeClick(data) {
      if(data.node === '#') {
        this.artifactDialogVisible = true
        eventBus.$emit('ArtifactClass:get', data.label, res => {
          this.form = res
        })
      }else {
        eventBus.$emit('select', data.node)
      }
    },
  },
  mounted() {
    eventBus.$on('graph:updated', that => {
      this.treeData = []
      this.treeData.push({label:'Artifacts', children:that.ArtifactClass.map(item => {
        return {label:item.label, node: '#'}
      })})
      this.treeData.push({label:'库', children: that.graph.getNodes().filter(item => Utils.getCellType(item)==='circle-node').map(item => {
        return {label:item.data.label, node: item}
      })})
      this.treeData.push({label:'服务', children: that.graph.getNodes().filter(item => Utils.getCellType(item).indexOf('service')!==-1).map(item => {
        return {label:item.data.label, node: item}
      })})
      let events = that.graph.getNodes().filter(item => Utils.getCellType(item)==='producer-node').map(item => {
        return {label:item.data.label + '(producer)', node: item}
      })
      let events2 = that.graph.getNodes().filter(item => Utils.getCellType(item)==='consumer-node').map(item => {
        return {label:item.data.label + '(consumer)', node: item}
      })
      events.push(...events2)
      this.treeData.push({label:'事件', children: events})
      
    })
  },
}
</script>

<style scoped>
.attribute-title {
  line-height: 2rem;
  height: 2rem;
  text-align: center;
  border-bottom: 1px solid #cecece;
}
.tag-list {
  display: flex;
  flex-wrap: wrap;
}
  .el-tag {
    margin-right: 10px;
    margin-top: 10px;
  }
</style>
